// pages/article/article.js
var app = getApp();
var ikz = require('../../Common/tools.js');
var WxParse= require('./../../../pages/wxParse/wxParse.js');

Page({
  data: {
    article_content:'',
    currentTab: 0, //预设当前项的值
    scrollLeft: 20, //tab标题的滚动条位置
    head_info: '',//所有头条信息
    page: 1,//页数
    limit: 6,//每页条数
    prodis: 'none',//底部提示是否显示
    showView: false, //发表弹窗
    inputValue: '',//弹窗的值
    bigclose: '',
    advert_info: '',//广告信息
    isFold: false,
    chid: '',//评论时本条消息id
    mid: '',//所评论的评论id
    isallzan: 2,//2可以操作点赞，3不可以，防止重复点击
    iscol: 2,//2可以操作收藏，3不可以，防止重复点击
    remarkshow: 'none', //不回复的情况下 点击遮罩层消失
    imgnum: 1, //banner第几张
    color_tou: '#fff',//头条字色
    // bg_tou: 'linear-gradient(-135deg, #0AC4BA 0%, #2BDA8E 100%)',//头条背景色
    color_bian: '#fff',//便民字色
    // bg_bian: '#fff',//便民背景色
    src1:'',   //头部图片切换
    src2:'',
    deletes:'none',
    selectid: '',//选择删除的信息id
  },

  // 置顶
  toTop: function () {
    wx.pageScrollTo({
      scrollTop: 0
    })
  },

  deshows: function (e) {
    var hid = e.currentTarget.dataset.hid;
    var selectid = hid;
    var deletess = this.data.deletes;
    if (deletess == 'none') {
      var deletes = 'block';
    } else {
      var deletes = 'none';
    }
    this.setData({
      deletes: deletes,
      selectid: selectid,
    })
  },
  //删除头条
  delInfo: function (e) {
    var that = this;
    that.setData({
      deletes: 'none'
    })
    var hid = e.currentTarget.dataset.hid; 
    var head_info = that.data.head_info;
    var data = {
      hid: hid,
    };
    wx.showModal({
      title: '提示',
      content: '您确认删除本信息吗？',
      success: function (res) {
        if (res.confirm) {
          var path = 'Index/delHead';
          ikz.request(path, data, true, (res) => {
            console.log(res);
            if (res.data.status == 'success') {
              wx.showToast({
                title: '删除信息成功！',
                duration: 2000
              });
              var z = -1;
              for (var i = 0; i < head_info.length; i++) {
                if (hid == head_info[i].head_id) {
                  z = i;
                }
              }
              if (z > -1) {
                head_info.splice(z, 1);
              };
              that.setData({
                head_info: head_info,
              })
            } else if (res.data.status == 'haveson') {
              wx.showModal({
                title: '提示',
                content: '本评论有子评论，不能删！',
                showCancel: false
              })
            } else if (res.data.status == 'nochom') {
              wx.showModal({
                title: '提示',
                content: '不是本人评论，不能删！',
                showCancel: false
              })
            } else {
              wx.showModal({
                title: '加载失败',
                content: '请稍后重试！',
                showCancel: false
              })
            }

          });
        } else if (res.cancel) {
          return false;
        }
      }
    })

  },
  

  // 点击改变样式
  changeStyle(e) {
    var hid = e.currentTarget.dataset.hid;
    var ctp = e.currentTarget.dataset.ctp;
    var that = this;
    var head_info = that.data.head_info;
    if(ctp == '1'){
      var con = 'content2';
    }else{
      var con = 'content';
    }
    for (var i = 0; i < head_info.length; i++) {
      if (hid == head_info[i].head_id) {
        head_info[i].class = con;
      }
    }
    that.setData({
      head_info: head_info,
    });
  },
  //选择分类
  chooseType: function (e) {
    var tid = e.currentTarget.dataset.tid;
    var data = {
      tid: tid,
    }
    if (tid == '1') {
      // this.setData({
      //   src1: '../../img/white.png',
      //   src2: '../../img/green2.png',
      //   color_tou: '#fff',//头条字色
      //   bg_tou: 'linear-gradient(-135deg, #0AC4BA 0%, #2BDA8E 100%)',//头条背景色
      //   color_bian: '#000',//便民字色
      //   bg_bian: '#fff',//便民背景色
      // })
    } else {
      // this.setData({
      //   src1:'../../img/green.png',
      //   src2: '../../img/white2.png',
      //   color_tou: '#000',//头条字色
      //   bg_tou: '#fff',//头条背景色
      //   color_bian: '#fff',//便民字色
      //   bg_bian: 'linear-gradient(-135deg, #0AC4BA 0%, #2BDA8E 100%)',//便民背景色
        
      // })
    }
    ikz.nav('../write/write', 'n', data);
  },

  //广告图片页
  toImg: function (e) {
    var id = e.currentTarget.dataset.id;
    var typ = 'ad';
    var com = 'home';
    var data = {
      id: id,
      typ: typ,
      com: com,
    }
    ikz.nav('../../Home/images/images', 'n', data);
  },

  // 溢出隐藏样式
  flodFn: function () {
    console.log('改变样式')
    this.setData({
      isFold: !this.isFold
    });
  },

  // 点击收藏
  collectTop: function (e) {
    var hid = e.currentTarget.dataset.hid;
    var that = this;
    var head_info = that.data.head_info;
    //防止重复点击
    that.setData({
      iscol: 3,
    });
    var data = {
      hid: hid,
    };
    var path = 'Index/collectHead';
    ikz.request(path, data, true, (res) => {
      console.log(res);
      if (res.data.status == 'success') {
        wx.showToast({
          title: '收藏成功！',
          duration: 2000
        });
        for (var i = 0; i < head_info.length; i++) {
          if (hid == head_info[i].head_id) {
            head_info[i].iscollect = 'yi';
          }
        }
        that.setData({
          head_info: head_info,
        });
      } else if (res.data.status == 'fails') {
        wx.showToast({
          title: '已收藏！',
          duration: 2000
        });
        for (var i = 0; i < head_info.length; i++) {
          if (hid == head_info[i].head_id) {
            head_info[i].iscollect = 'yi';
          }
        }
        that.setData({
          head_info: head_info,
        });
      } else {
        wx.showModal({
          title: '加载失败',
          content: '请稍后重试！',
          showCancel: false
        });
      }
      that.setData({
        iscol: 2,
      });
    });
  },
  // 取消收藏
  collectTops: function (e) {
    var hid = e.currentTarget.dataset.hid;
    var that = this;
    var head_info = that.data.head_info;
    //防止重复点击
    that.setData({
      iscol: 3,
    });
    var data = {
      hid: hid,
    };
    var path = 'Index/collectCancle';
    ikz.request(path, data, true, (res) => {
      console.log(res);
      if (res.data.status == 'success') {
        wx.showToast({
          title: '取消收藏成功！',
          duration: 2000
        });
        for (var i = 0; i < head_info.length; i++) {
          if (hid == head_info[i].head_id) {
            head_info[i].iscollect = 'wei';
          }
        }
        that.setData({
          head_info: head_info,
        });
      } else if (res.data.status == 'fails') {
        wx.showToast({
          title: '已取消收藏！',
          duration: 2000
        });
        for (var i = 0; i < head_info.length; i++) {
          if (hid == head_info[i].head_id) {
            head_info[i].iscollect = 'wei';
          }
        }
        that.setData({
          head_info: head_info,
        });
      } else {
        wx.showModal({
          title: '加载失败',
          content: '请稍后重试！',
          showCancel: false
        });
      }
      that.setData({
        iscol: 2,
      });
    });
  },
  //本条消息点赞
  goHeadLike: function (e) {
    var hid = e.currentTarget.dataset.hid;
    var that = this;
    var head_info = that.data.head_info;
    //防止重复点击
    that.setData({
      isallzan: 3,
    });
    var data = {
      hid: hid,
    };
    var path = 'Index/goHeadLike';
    ikz.request(path, data, true, (res) => {
      console.log(res);
      if (res.data.status == 'success') {
        for (var i = 0; i < head_info.length; i++) {
          if (hid == head_info[i].head_id) {
            head_info[i].islike = 3;
            head_info[i].head_like = parseInt(head_info[i].head_like) + 1;
          }
        }
        that.setData({
          head_info: head_info,
        });
      } else {
        wx.showModal({
          title: '加载失败',
          content: '请稍后重试！',
          showCancel: false
        })
      }
      that.setData({
        isallzan: 2,
      });
    });
  },
  //本条取消消息点赞
  noHeadLike: function (e) {
    var hid = e.currentTarget.dataset.hid;
    var that = this;
    var head_info = that.data.head_info;
    //防止重复点击
    that.setData({
      isallzan: 3,
    });
    var data = {
      hid: hid,
    };
    var path = 'Index/noHeadLike';
    ikz.request(path, data, true, (res) => {
      console.log(res);
      if (res.data.status == 'success') {
        for (var i = 0; i < head_info.length; i++) {
          if (hid == head_info[i].head_id) {
            head_info[i].islike = 2;
            head_info[i].head_like = parseInt(head_info[i].head_like) - 1;
          }
        }
        that.setData({
          head_info: head_info,
        });
      } else {
        wx.showModal({
          title: '加载失败',
          content: '请稍后重试！',
          showCancel: false
        })
      }
      that.setData({
        isallzan: 2,
      });
    });
  },
  
  

  onLoad: function (options) {
    ikz.over(this);
    var that = this;
    that.setData({
      deletes: 'none',
      article_content: WxParse.wxParse('article_content', 'html', that.data.article_content, that, 5)
    })
    // article_content: WxParse.wxParse('article_content', 'html', options.data.article_content, that, 5)
    // var bg_today = 'linear-gradient(-135deg, #0AC4BA 0%, #2BDA8E 100%)';
    // var co_today = '#fff';
    // var bg_yesterday = '#fff';
    // var co_yesterday = '#000';
    var page = 1;
    var limit = that.data.limit;
    var path = 'Index/index';
    var data = {
      page: page,
      limit: limit,
    }
    if (app.globalData.openUserInfo == '') {
      var islogin = false;
    } else {
      var islogin = true;
    }
    ikz.request(path, data, islogin, (res) => {
      if (res.data.status == 'success') {
        console.log(res.data.head_top_info);
        console.log(232323);
        that.setData({
          head_info: res.data.head_info,//所有头条信息
          head_top_info: res.data.head_top_info,//所有头条信息
          advert_info: res.data.advert_info,//所有广告信息
          // bg_today: bg_today, //今日信息背景色
          // co_today: co_today, //今日信息字体色
          // bg_yesterday: bg_yesterday, //昨日信息背景色
          // co_yesterday: co_yesterday, //昨日信息字体色
          page: page,
          remarkshow: 'none',
          src1: '../../img/white.png',
          src2: '../../img/white2.png',
          deletes: 'none'
        });
        ikz.hide(that);
      } else {
        wx.showModal({
          title: '加载失败',
          content: '请稍后重试！',
          showCancel: false
        })
      }

    });
  },

  // 详情页
  toContent: function (e) {
    var that = this;
    var hid = e.currentTarget.dataset.hid;
    wx.navigateTo({
      url: '../pageDetail/pageDetail?' + 'hid=' + hid,
    })

  },
  delclose:function(){
    var that=this;
     that.setData({
      deletes: 'none'
    }) 
  },



  toTop: function () {
    wx.pageScrollTo({
      scrollTop: 0
    })
  },

  onReady: function () {

  },


  //数字转换
  changeNum(num) {
    if (num > 0 && num < 10000) {
      num = num / 100;
      num = Math.ceil(num) / 10;
      var newnum = num + 'k';
    } else if (num >= 10000) {
      num = num / 1000;
      num = Math.ceil(num) / 10;
      var newnum = num + 'w';
    } else {
      var newnum = 0;
    }
    return newnum;
  },

  onShow: function () {


  },
  onHide: function () {
    // 页面隐藏
  },
  onUnload: function () {
    // 页面关闭
  },

  onPullDownRefresh: function () {
    wx.showLoading({
      title: '刷新中...',
    });
    var that = this;
    var page = 1;
    var limit = that.data.limit;
    var path = 'Index/index';
    var data = {
      page: page,
      limit: limit,
    }
    if (app.globalData.openUserInfo == '') {
      var islogin = false;
    } else {
      var islogin = true;
    }
    ikz.request(path, data, islogin, (res) => {
      if (res.data.status == 'success') {
        that.setData({
          head_info: res.data.head_info,//所有头条信息
          head_top_info: res.data.head_top_info,//所有头条信息
          advert_info: res.data.advert_info,//所有广告信息
          page: page,
        });
        // 隐藏导航栏加载框  
        wx.hideNavigationBarLoading();
        // 停止下拉动作  
        wx.stopPullDownRefresh();
        wx.hideLoading()
      } else {
        wx.showModal({
          title: '加载失败',
          content: '请稍后重试！',
          showCancel: false
        })
      }

    });
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    wx.showLoading({
      title: '玩命加载中',
    })
    var that = this;
    var page = that.data.page + 1;
    var limit = that.data.limit;
    var old_info = that.data.head_info;
    that.setData({
      page: page,
    })
    var path = 'Index/index';
    var data = {
      page: page,
      limit: limit,
    }
    if (app.globalData.openUserInfo == '') {
      var islogin = false;
    } else {
      var islogin = true;
    }
    ikz.request(path, data, islogin, (res) => {

      if (res.data.status == 'success') {
        var new_info = res.data.head_info;
        if (new_info.length) {
          var all_info = old_info.concat(new_info);
          var prodis = 'none';
        } else {
          var all_info = old_info;
          var prodis = 'block';
        }
        that.setData({
          head_info: all_info,//所有头条信息
          prodis: prodis,
        })
        // 隐藏导航栏加载框  
        wx.hideNavigationBarLoading();
        // 停止下拉动作  
        wx.stopPullDownRefresh();
        wx.hideLoading()
      } else {
        wx.showModal({
          title: '加载失败',
          content: '请稍后重试！',
          showCancel: false
        })
      }

    });
    wx.hideLoading();
  },
  getRandomColor() {
    let rgb = [];
    for (let i = 0; i < 3; ++i) {
      let color = Math.floor(Math.random() * 256).toString(16);
      color = color.length == 1 ? '0' + color : color;
      rgb.push(color);
    }
    return '#' + rgb.join('');
  },
})
